package com.lanou.sinasports.fragment;

import android.content.Context;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.sinasports.R;

import com.lanou.sinasports.adapter.NewRecomAdapter;
import com.lanou.sinasports.beans.GsonNewsRecom;

import com.lanou.sinasports.beans.NewRecom;
import com.lanou.sinasports.utils.GsonRequest;

import com.lanou.sinasports.utils.VolleySingleton;
import com.lanou.sinasports.utils.XListView;
import com.lanou.sinasports.utils.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 16/1/15.
 */
public class NewsRecommandFragment extends Fragment implements IXListViewListener {
    private Context context;

    private NewRecomAdapter adapter;
    private List<NewRecom> data;
    private static VolleySingleton queue;
    private String url="http://platform.sina.com.cn/sports_client/cre?app_key=2586208540&_version__" +
            "=3.2.0.14&__os__=android&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_" +
            "show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&length=20&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";
    private final String TAG="XListViewActivity";
    private XListView mListView;

    private ArrayList<String> items = new ArrayList<String>();
    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_news_recommend,container,false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mListView= (XListView) view.findViewById(R.id.xListView);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mListView.setPullLoadEnable(true);
        mListView.setXListViewListener(this);
        mListView.setPullLoadEnable(true);
        mListView.setPullRefreshEnable(true);

        data=new ArrayList<>();
        mHandler=new Handler();
        queue=VolleySingleton.getInstance();

        GsonRequest<GsonNewsRecom> request=new GsonRequest<GsonNewsRecom>(url, GsonNewsRecom.class, new Response.Listener<GsonNewsRecom>() {
            @Override
            public void onResponse(GsonNewsRecom gsonNewsRecom) {

                int size=gsonNewsRecom.getResult().getData().getFeed().getData().size();
                for(int i=0;i<size;i++){
                   NewRecom newRecom=new NewRecom();
                    int comTotal=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getComment_total();
                    String id=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getCategoryid();
                    String stitle=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getStitle();
                    String summary=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getWapsummary();
                    String url=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getUrl();

                    for(int j=0;j<gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getImages().size();j++) {
                            String image = gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getImages().get(j).getU();
                            newRecom.setImages(image);
                    }
                    newRecom.setComment_total(comTotal);
                    newRecom.setCategoryid(id);
                    newRecom.setStitle(stitle);
                    newRecom.setWapsummary(summary);
                    newRecom.setUrl(url);


                    data.add(newRecom);

                }
                adapter=new NewRecomAdapter(getActivity(),data);

                mListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        queue._addRequest(request);


    }
    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }


    @Override
    public void onRefresh() {
        final List<NewRecom> ss = new ArrayList<>();
        Log.e("xxxx", "------refresh---");

        GsonRequest<GsonNewsRecom> request=new GsonRequest<GsonNewsRecom>(url, GsonNewsRecom.class, new Response.Listener<GsonNewsRecom>() {
            @Override
            public void onResponse(GsonNewsRecom gsonNewsRecom) {

                int size=gsonNewsRecom.getResult().getData().getFeed().getData().size();
                for(int i=0;i<size;i++){
                    NewRecom newRecom=new NewRecom();
                    int comTotal=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getComment_total();
                    String id=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getCategoryid();
                    String stitle=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getStitle();
                    String summary=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getWapsummary();
                    String url=gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getUrl();

                    for(int j=0;j<gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getImages().size();j++) {
                        String image = gsonNewsRecom.getResult().getData().getFeed().getData().get(i).getImages().get(j).getU();
                        newRecom.setImages(image);
                    }
                    newRecom.setComment_total(comTotal);
                    newRecom.setCategoryid(id);
                    newRecom.setStitle(stitle);
                    newRecom.setWapsummary(summary);
                    newRecom.setUrl(url);


                    ss.add(newRecom);

                }
                Log.e("xxx", "加入数据倒适配器");
               adapter.addData(ss);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });



        queue._addRequest(request);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                onLoad();
            }
        },2000);


//
//        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                start = ++refreshCnt;
//                items.clear();
//                geneItems();
//                // mAdapter.notifyDataSetChanged();
//                adapter = new NewRecomAdapter(context,data);
//                mListView.setAdapter(adapter);
//
//
//                onLoad();
//
//            }
//
//        }, 2000);
    }


        @Override
        public void onLoadMore() {

            Log.e("x", "-----onload--");
//            Log.i(TAG, "���ظ���");
//            mHandler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//
//                    adapter.notifyDataSetChanged();
//                    onLoad();
//                }
//            }, 2000);
        }

}

